

do_by_prof <- function(prof,... ) {
  table_2_func(name = tools::toTitleCase(gsub("_"," ",prof)),
               data_filterd = dt_adm_month[new_profession == prof,
                                           .(actual_cost = sum(actual_cost,na.rm = T )),
                                           by = .(id_var,
                                                  months_after_dx_0 = month_from_index)],
               ... )[[1]]
}

do_by_procedure <- function(cat,... ) {
  table_2_func(name = tools::toTitleCase(gsub("_"," ",cat)),
               data_filterd = data.table(dt_adm_proc_month)[type == cat,
                                           .(actual_cost = sum(actual_cost,na.rm = T )),
                                           by = .(id_var,
                                                  months_after_dx_0 = month_from_index)],
               ... )[[1]]
}


#Average Monthly Spending of Cancer Patients with Alternative Admission Grouping

do_Alternative_Adm_Grouping <- function(filename,
                         intensity_wards = c("oncology","internal_medicine","geriatry","rehabilitation"),
                         ...) {
  
  
  full_table <-   rbind(table_2_func(name = "Total", data_filterd=make_slice_cost_month(dt_cost_for_exhibits_cancer_month), ... )[[1]],
                        table_2_func(name = "All Inpatient", data_filterd = dt_adm_month[,.(actual_cost = sum(actual_cost,na.rm = T )),
                                                                                              by = .(id_var,
                                                                                                     months_after_dx_0 = month_from_index)], ... )[[1]],
                        table_2_func(name = "Planned"      , data_filterd = dt_adm_month[category!="hospitalization_urgent",
                                                                                               .(actual_cost = sum(actual_cost,na.rm = T )),
                                                                                               by = .(id_var,
                                                                                                      months_after_dx_0 = month_from_index)], ... )[[1]],
                        table_2_func(name = "Unplanned"    , data_filterd = dt_adm_month[category=="hospitalization_urgent",
                                                                                               .(actual_cost = sum(actual_cost,na.rm = T )),
                                                                                               by = .(id_var,
                                                                                                      months_after_dx_0 = month_from_index)], ... )[[1]],
                        table_2_func(name = "Low Intensity", data_filterd = dt_adm_month[profession%in%intensity_wards,
                                                                                                .(actual_cost = sum(actual_cost,na.rm = T )),
                                                                                                by = .(id_var,
                                                                                                       months_after_dx_0 = month_from_index)], ... )[[1]],
                        table_2_func(name = "High Intensity", data_filterd = dt_adm_month[!profession%in%intensity_wards,
                                                                                                .(actual_cost = sum(actual_cost,na.rm = T )),
                                                                                                by = .(id_var,
                                                                                                       months_after_dx_0 = month_from_index)], ... )[[1]],
                        table_2_func(name = "Procedure Based", data_filterd = dt_adm_month[category == "differential",
                                                                              .(actual_cost = sum(actual_cost,na.rm = T )),
                                                                              by = .(id_var,
                                                                                     months_after_dx_0 = month_from_index)], ... )[[1]],
                       table_2_func(name = "Per diem", data_filterd = dt_adm_month[category != "differential",
                                                                                   .(actual_cost = sum(actual_cost,na.rm = T )),
                                                                                   by = .(id_var,
                                                                                          months_after_dx_0 = month_from_index)], ... )[[1]],
                      lapply(unique(dt_adm$new_profession) , do_by_prof,...) %>% bind_rows(),
                      table_2_func(name = "All Clalit Owned Inpatient", data_filterd = dt_adm_proc_month[type!="External",
                                                                                    .(actual_cost = sum(actual_cost,na.rm = T )),
                                                                                    by = .(id_var,
                                                                                           months_after_dx_0 = month_from_index)], ... )[[1]],
                      lapply(unique(dt_adm_proc_month$type) , do_by_procedure,...) %>% bind_rows(),
                      table_2_func(name = "Other Services:",
                                   data_filterd =   make_slice_cost_month(dt_cost_for_exhibits_cancer_month[!main_cat%like%"Inpatient"]),... )[[1]],
                      table_2_func(name = "Drugs", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Drugs"]), ... )[[1]],
                      table_2_func(name = "Outpatient", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Outpatient"]), ... )[[1]],
                      table_2_func(name = "Imaging", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Imaging"]), ... )[[1]],
                      table_2_func(name = "Other", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Others"]), ... )[[1]]
  )
                      
                                                                                          
  orig_full<-copy(full_table)
  
  full_table[,sh_total := format(round((Difference/full_table[1]$Difference)*100 ,1 ),nsmall = 1)]
  
  full_table[, c(2,3,4,5) :=  lapply(.SD,scales::comma,accuracy=1), .SDcols = c(2,3,4,5)]
  
  # sort categories 
  table_A14_pub<- orig_full[-1,][c(1,4,9,13,15,14,5,10,17,16,11,18,8,12,3,2,7,6,19,21,22,24,23)]
  table_A14_pub[,sh_total := format(round((Difference/table_A14_pub[1]$Difference)*100 ,1 ),nsmall = 1)]
  
  table_A14_pub[19:23,sh_total := format(round((Difference/table_A14_pub[19]$Difference)*100 ,1 ),nsmall = 1)]
  table_A14_pub[, c(2,3,4,5) :=  lapply(.SD,scales::comma,accuracy=1), .SDcols = c(2,3,4,5)]
  
  names(table_A14_pub) <- c("","(1)", "(2)", "(3)","(4)","(5)")
  
  table_A14_pub %>% 
    kable("latex", booktabs = T, align = c("l",rep("r", ncol(.)-1))) %>%
    kable_styling(full_width = F ) %>%
    add_header_above(c("Category",
                       "Unweighted",
                       "Reweighted by\nDecedent Risk", 
                       "",
                       "Decedent -\n Survivor\n(Reweighted)",
                       "Percent of \n Total Difference"),
                     underline = F,line = F) %>%
    add_header_above(c(" " = 1, "Survivor" = 2, "Decedent" = 1, "Difference" = 2),bold = T) %>% 
    pack_rows("A. By Ward", 2, 14) %>%
    add_indent(3:6) %>%
    add_indent(8:14) %>% 
    pack_rows("B. By Planned Status", 15, 16) %>% 
    pack_rows("C. By Billing Method", 17, 18) %>% 
    pack_rows("D. By Main Procedure", 19, 23) %>% 
    add_indent(20:23) %>% 
    row_spec(0, align = "c")  %>%
    write("Ave_Monthly_Spending_Cancer_Alternative_Admission_Grouping.tex")
  
  write.csv(table_A14_pub,"Ave_Monthly_Spending_Cancer_Alternative_Admission_Grouping.csv")
  
  
  return(list(full_table,orig_full))
}



